﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// PageBase 的摘要说明
/// </summary>
public class PageBase : System.Web.UI.Page
{
    public User _User;

    public bool isRequestLogin = false;                 // 是否需要登录
    public bool SecondLevelPassword = false;            // 是否需要二级登录
    public string RequestLoginPage = "";                // 需要登录后，转跳到 Login.aspx 页面，登录后，会按此页面自动定位回来
    public bool isFramePage = false;                    // 是否框架内的页面。如果是框架内的页面，转跳登录将在 _top 进行。

    public string RequestCompetences = "";              // 需要的权限列表，用 [Administrator][Competence][...]... 表示

    public bool isSurrogatePage = false;                // 代理上平台的页面，设置为 True

    //public bool isPromotionerPage = false;              // 联盟商平台的页面，设置为 True

    public bool isArrowDesign = false;                  // 设置此变量为 True，页面左上角将出现设计页面的按钮(当用户有管理权限时)

    public PageBase()
    {
    }

    protected override void OnLoad(EventArgs e)
    {
        #region 设置 Page Title、Keyword

        try
        {
            //this.Page.Title = PageTitle == "" ? _Site.Name : PageTitle;
        }
        catch { }

        HtmlMeta hm = new HtmlMeta();
        hm.Name = "keywords";
        //hm.Content = _Site.PageKeywords;

        try
        {
            this.Page.Header.Controls.Add(hm);
        }
        catch { }


        #endregion

        // 总站才能操作的页面
        //if (isMasterSiteOnly && (Level != MasterSite))
        //{
        //    PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", "PageBase");

        //    return;
        //}

        // 获取用户

        _User = GetSessionUser();
        
        if (isRequestLogin && (_User == null))
        {
            PublicFunction.GoLogin(RequestLoginPage, isFramePage);

            return;
        }

        bool SystemClose = PublicFunction.GetOptionsAsBoolean("系统紧急关闭", false);

        if (SystemClose)
        {
            if (_User != null)
            {
                if (!_User.Competences.IsOwnedCompetences(Competences.Administrator))
                {
                    Response.Redirect("SystemStop.aspx", true);

                    return;
                }
            }
            else
            {
                if (Request["Key"] != "Admin")
                {
                    Response.Redirect("SystemStop.aspx", true);

                    return;
                }
            }
        }

        string UserOfficeOK = (string)ShoveEL._Web.Session.GetSessionAsString(ShoveEL._Web.WebConfig.GetAppSettingsString("pSystemPreFix") + "UserOffice", "");

        if (isRequestLogin && (_User != null) && SecondLevelPassword && (RequestCompetences == "") && (!_User.Competences[Competences.Administrator]) && (UserOfficeOK != PublicFunction.EncryptPassword("UserOfficeOKZXSystem")))
        {
            Response.Redirect("~/BalanceCenter/LoginOffice.aspx?SubPage=" + RequestLoginPage, true);

            return;
        }

        #region 判断权限
        
        if (isRequestLogin && (RequestCompetences != "") && (!_User.Competences.IsOwnedCompetences(RequestCompetences)))
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", "PageBase");

            return;
        }

        #endregion

        base.OnLoad(e);
    }

    public User GetSessionUser()
    {
        string SessionUserID = ShoveEL._Web.Session.GetSessionAsString(ShoveEL._Web.WebConfig.GetAppSettingsString("pSystemPreFix") + "User", "");

        if (SessionUserID == "")
        {
            return null;
        }

        try
        {
            SessionUserID = ShoveEL._Security.Encrypt.UnEncryptString(PublicFunction.GetCallCert(), SessionUserID);
        }
        catch
        {
            return null;
        }

        if (SessionUserID == "")
        {
            return null;
        }

        long UserID = ShoveEL._Convert.StrToLong(SessionUserID, -1);

        if (UserID < 1)
        {
            return null;
        }

        User user = new User();
        user.ID = UserID;
        string ReturnDescription = "";

        int Result = user.GetUserInfoByID(ref ReturnDescription);

        if (Result < 0)
        {
            return null;
        }

        return user;
    }
}
